package com.crm.system.controller.orderManager;

import cn.hutool.core.util.ObjectUtil;
import com.crm.common.model.DataResponse;
import com.crm.common.model.DhPageInfo;
import com.crm.common.model.DhResponse;
import com.crm.common.model.Enum.ResponseCodeEnum;
import com.crm.model.entity.orderManager.CustomerOrderInvoice;
import com.crm.model.entity.orderManager.CustomerOrderInvoiceLog;
import com.crm.model.qo.orderManager.OrderInvoiceQO;
import com.crm.model.vo.orderManager.ApplyOrderInvoiceInfoVO;
import com.crm.model.vo.orderManager.OrderInvoiceVO;
import com.crm.model.vo.orderManager.PtInvoiceBlueResultVO;
import com.crm.system.service.orderManager.CustomerOrderInvoiceService;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;

/**
 * @Author:chaifuyou
 * @Date: 2021/4/26 11:40
 */
@RestController
@RequestMapping("/orderInvoice")
@Slf4j
public class CustomerOrderInvoiceController {

    @Autowired
    private CustomerOrderInvoiceService customerOrderInvoiceService;

    /**
     * 财务-新增开票信息记录
     * @param customerOrderInvoiceLog
     * @return
     */
    @PostMapping("/addOrderInvoiceLog")
    private DataResponse addOrderInvoiceLog(@RequestBody List<CustomerOrderInvoiceLog> customerOrderInvoiceLog){
        PtInvoiceBlueResultVO resultVO = customerOrderInvoiceService.addOrderInvoiceLog(customerOrderInvoiceLog);
        if ("0000".equals(resultVO.getCode())){
            return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
        }else {
            DataResponse dataResponse = new DataResponse();
            dataResponse.setMessage(resultVO.getCode());
            dataResponse.setMessage(resultVO.getMsg());
            return dataResponse;
        }
    }

    /**
     * 新增发票申请
     * @param customerOrderInvoice
     * @return
     */
    @PostMapping("/addOrderInvoice")
    private DataResponse addOrderInvoice(@RequestBody CustomerOrderInvoice customerOrderInvoice){
        if (customerOrderInvoice.getInvoiceType() != 2){
            DataResponse response = new DataResponse();
            response.setCode("00001");
            response.setMessage("申请失败，目前系统只能申请电子发票");
            return response;
        }
        customerOrderInvoiceService.addOrderInvoice(customerOrderInvoice);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 查看发票申请详情
     * @param chsCustomerOrderInvoiceId
     * @return
     */
    @PostMapping("/selectApplyOrderInvoiceInfo")
    private DataResponse selectApplyOrderInvoiceInfo(@RequestParam String chsCustomerOrderInvoiceId){
        ApplyOrderInvoiceInfoVO infoVO = customerOrderInvoiceService.selectApplyOrderInvoiceInfo(chsCustomerOrderInvoiceId);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),infoVO);
    }

    /**
     * 查询开票记录
     * @param request
     * @param pageIndex
     * @param pageSize
     * @param sortName
     * @param sortOrder
     * @param qo
     * @return
     */
    @GetMapping("/selectOrderInvoiceList")
    private DataResponse selectOrderInvoiceList(HttpServletRequest request,
                                                @RequestParam(value = "pageIndex", required = true) Integer pageIndex,//页码
                                                @RequestParam(value = "pageSize", required = true) Integer pageSize,//分页大小
                                                @RequestParam(value="sortName",required = false) String sortName,//排序列名字
                                                @RequestParam(value="sortOrder",required = false) String sortOrder,//排序规则(asc/desc
                                                OrderInvoiceQO qo){
        DhPageInfo<OrderInvoiceVO> list = customerOrderInvoiceService.selectOrderInvoiceList(pageIndex,pageSize,sortName,sortOrder,qo);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode(),list);
    }



    /**
     * 修改开票审批状态
     * @param id 发票表主键
     * @param type
     * @return
     */
    @GetMapping("/updateExamineStatus")
    private DataResponse updateExamineStatus(@RequestParam String id,@RequestParam String type){
        CustomerOrderInvoice customerOrderInvoice = customerOrderInvoiceService.selectByPrimaryKey(id);
        if (Objects.nonNull(customerOrderInvoice) && 0 != customerOrderInvoice.getExamineStatus()){
            return new DhResponse(ResponseCodeEnum.ORDER_INVOICE_EDITED.getResponseCode());
        }
        customerOrderInvoiceService.updateExamineStatus(id,type);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    /**
     * 删除开票信息
     * @param id 发票表主键
     * @return
     */
    @PostMapping("/delOrderInvoice")
    private DataResponse delOrderInvoice(String id){
        CustomerOrderInvoice customerOrderInvoice = customerOrderInvoiceService.selectByPrimaryKey(id);
        if (Objects.nonNull(customerOrderInvoice) && 0 != customerOrderInvoice.getExamineStatus()){
            return new DhResponse(ResponseCodeEnum.ORDER_INVOICE_EDITED_DEL.getResponseCode());
        }
        customerOrderInvoiceService.delOrderInvoice(id);
        return new DhResponse(ResponseCodeEnum.OK.getResponseCode());
    }

    public static String doJsonPost(String url, String json,String authValue) {
        String result =null;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        ResponseHandler<String> responseHandler =new BasicResponseHandler();
        try {
            HttpPost httpPost =new HttpPost(url);
            StringEntity requestEntity =new StringEntity(json,"utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setHeader("Content-type","application/json");
            if(!ObjectUtil.isNull(authValue)){
                httpPost.setHeader("Authorization","Bearer "+authValue);
            }
            httpPost.setEntity(requestEntity);
            result = httpClient.execute(httpPost, responseHandler);
        }catch (Exception e) {
            System.out.println(e.getMessage());
        }finally {
            try {
                httpClient.close();
            }catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
        return result;
    }
}
